package com.it.octoo.source;

import cn.hutool.core.util.URLUtil;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * <p>
 * 角色数据加载
 * </p>
 *
 * @author miaoxiao
 * @since 2019-12-20
 */
@Service
public class CustomerSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        String requestUrl = ((FilterInvocation) object).getRequestUrl();
        String url = URLUtil.getPath(requestUrl);
        if(url.equals("/config/get")){
            return SecurityConfig.createList("ROLE_USER", "ROLE_ADMIN");
        }else{
            return SecurityConfig.createList("ROLE_ROOT");
        }
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }



    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }

}
